const index = require('../config/index.js')
// 获取上架商品
export function getStatusProducts(page, limit) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + `putawayProduct?page=${page}&limit=${limit}`,
      method: 'get',
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
// 根据id获取商品
export function getProductById(id) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + `product/${id}`,
      method: 'GET',
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
// 获取热门商品
export function getHotProducts() {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + 'hotProducts',
      method: 'GET',
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
// 获取最新商品
export function getNewProducts() {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + 'newProducts',
      method: 'GET',
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
// 根据商品类别获取商品 
export function getProductsByCategory(typeid) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + `products/${typeid}`,
      method: 'get',
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
// 根据搜索关键字获取商品
export function getProductsByKeyWords(query) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + 'products',
      method: 'GET',
      data: query,
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
// 根据id修改商品库存
export function updateInventory(id, data) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + `product/inventory/${id}`,
      method: 'PUT',
      data,
      header: {
        'Authrization': 'Bearer ' + index.token
      },
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
// 根据筛选条件获取商品
export function getProductsByFilter(name, typeid, column) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: index.api + `products/filter?name=${name}&typeid=${typeid}&column=${column}`,
      method: 'GET',
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}
export default {
  getStatusProducts,
  getProductById,
  getHotProducts,
  getNewProducts,
  getProductsByCategory,
  getProductsByKeyWords,
  updateInventory,
  getProductsByFilter
}